<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 订单结算商品ajax类
 * @package  ajax/comm/bill
 * @author   allen.qiang
 */

quanli_require('module/base/goods/quanliGoods.class.php');
quanli_require('module/base/system/itemHandler.class.php');

class ajax_checkoutGoods {

    private $bill_array; //购物车数组

    private $pcustID; //会员号

    private $orders_code; //下单编号

    private $source; //来源

    public function execute() {

        if (!isset($_REQUEST['source']) || !in_array($_REQUEST['source'], array('backend', 'web', 'wap', 'weixin', 'app'))) {
            $this->toReturn('参数错误');
        }
        $this->source = $_REQUEST['source'];

        //判断会员号
        if (!isset($_REQUEST['pcustID']) || empty($_REQUEST['pcustID'])) {
            $this->toReturn('参数错误');
        }
        $this->pcustID = $_REQUEST['pcustID'];

        $this->orders_code = $_REQUEST['orders_code'];

        $this->getBillArray();

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //查询三级商品信息
                    $this->goodsDetail();
                    break;

                case 2: //查询二级商品信息
                    $this->goodsList();
                    break;

                case 3: //更新购物车商品
                    $this->updateGeneralGoods();
                    break;

                case 4: //删除购物车商品
                    $this->deleteGeneralGoods();
                    break;

                case 6: //检查配送时间
                    $this->checkDeliveryTime();
                    break;

                case 7: //商品配件修改
                    $this->fittingsEdit();
                    break;

                case 8: //手动调价
                    $this->adjustPrice();
                    break;

                default:
                    returnJsonData('参数错误');
                    break;

            }

        } else {

            returnJsonData('参数错误');

        }

    }

    /**
     * 返回信息
     * @param string $message
     * @param int $code
     * @param mixed $data
     */
    private function toReturn( $message, $code = 400, $data = '' ) {

        die( json_encode( array( 'code' => $code, 'message' => $message, 'data' => $data ) ) );

    }

    /**
     * 获得购物车数据
     */
    private function getBillArray() {

        global $G_SHOP;

        $this->bill_array = $G_SHOP->sessionCache_get( 'checkout_backend_' . $this->orders_code, true );

    }

    /**
     * 保存购物车数据
     */
    private function setBillArray( $data = null ) {

        global $G_SHOP;

        $G_SHOP->sessionCache_set( 'checkout_backend_' . $this->orders_code, $this->bill_array, 0, true);

        returnJsonData('ok', 200, $data);

    }

    /**
     * 生成购物车商品数组结构
     * @param array $goods_array 商品数组
     * @param int $goods_amount 购买数量
     * @param int $type 类型：1、普通商品 2、赠品 6、闪购
     * @return array
     */
    private function createCartGoodsStructure( $goods_array, $goods_amount = 1, $type = 1 ) {

        if ($type == 1) {
            $goods_array['goods_from'] = 1;
            $goods_array['goods_groupID'] = 0;
            $goods_array['price'] = $goods_array['salePrice'];
        } else if ($type == 2) {
            $goods_array['addPoints'] = 0;
            $goods_array['salePoints'] = 0;
            $goods_array['goodsWeight'] = 0;
            $goods_array['goodsLength'] = 0;
            $goods_array['goodsWidth'] = 0;
            $goods_array['goodsHeight'] = 0;
        } else if ($type == 6) {
            $goods_array['goods_from'] = 6;
            $goods_array['goods_groupID'] = 0;
            $goods_array['addPoints'] = 0;
            $goods_array['salePoints'] = 0;
        }else if( $type == 5 ){
            $goods_array['goods_groupID'] = 0;
            $goods_array['goods_from'] = 5;
        }

        return array (
            'goodsID' => $goods_array['goodsID'],
            'postID' => $goods_array['postID'],
            'cityID' => $goods_array['cityID'],
            'goodsName' => $goods_array['goodsName'],
            'spec' => $goods_array['spec'],
            'goods_amount' => $goods_amount,
            'salePrice' => $goods_array['salePrice'],
            'price' => $goods_array['price'],
            'salePoints' => $goods_array['salePoints'],
            'goodsWeight' => $goods_array['goodsWeight'],
            'goods_from' => $goods_array['goods_from'],
            'goods_groupID' => $goods_array['goods_groupID'],
            'goods_status' => 1,
            'goodsMainPic' => $goods_array['goodsMainPic'],
            'bigCategoryID' => $goods_array['bigCategoryID'],
            'midCategoryID' => $goods_array['midCategoryID'],
            'smallCategoryID' => $goods_array['smallCategoryID'],
            'product_code' => $goods_array['product_code'],
            'syncopate' => $goods_array['syncopate'],
            'birthcard' => $goods_array['birthcard'],
            'twoPostID' => $goods_array['twoPostID'],
            'marketPrice' => $goods_array['marketPrice'],
            'startDeliver' => $goods_array['startDeliver'],
            'endDeliver' => $goods_array['endDeliver'],
            'deliverSpace' => $goods_array['deliverSpace'],
            'cutTime' => $goods_array['cutTime'],
            'goods_weight' => $goods_array['goodsWeight'] ? $goods_array['goodsWeight'] : 0,
            'promotion' => null
        );

    }

    /**
     * 查询三级商品信息
     */
    private function goodsDetail() {

        //检查商品编号
        if(!isset($_REQUEST['choose_data']) || !is_array($_REQUEST['choose_data']) || empty($_REQUEST['choose_data'])){
            returnJsonData('商品编号为空');
        }

        if(!isset($_REQUEST['cityID']) || empty($_REQUEST['cityID'])){
            returnJsonData('参数错误');
        }

        //add by guojun.song  2015-06-08   start
        //加载类文件
        quanli_require('module/base/bill/billSearch.class.php');
        //实例订单类
        $billSearch = new billSearch();
        //查询订单信息
        $args_array = array(
            'bill_no' => $_REQUEST['bill_no']
        );
        $bill_goods_array = $billSearch->searchBillGoods( $args_array );
        $already_nums = 10 - count($bill_goods_array);

        $jindannapolun = 0;
        foreach($_REQUEST['choose_data'] as $v){
               if(in_array($v['product_code'],array('n0101','n0102','n0103','n0105'))){
                   $jindannapolun += $v['amount'];
               }
        }

        if($jindannapolun > $already_nums){
           returnJsonData('该商品每单限购10份,请修改数量');
        }
        //add by guojun.song  2015-06-08   end

        global $G_SHOP;

        quanli_require('module/base/goods/goodsFittings.class.php');
        quanli_require('module/base/promotion/quanliCard.class.php');

        //实例商品类
        $quanliGoods = new quanliGoods( $G_SHOP->DBCA );

        //实例化现金卡类
        $quanliCard = new quanliCard( $G_SHOP->DBCA );

        //实例化商品配件类
        $goodsFittings = new goodsFittings($G_SHOP->DBCA);

        //生成商品编号数组
        $product_code_array = $_REQUEST['choose_data'];

        //初始化返回商品有关数组
        $return_array = array(
            'goods' => array(),
            'send_date' => date("Y-m-d"),
            'card_array' => array(),
        );


        //循环商品数组，查询商品信息
        foreach ($product_code_array as &$product_code) {

            $arg_array = array(
                'product_code' => $product_code['product_code'],
                'cityID' => $_REQUEST['cityID'],
                'forsale' => 1
            );
            //试吃订单
            if(isset($this->bill_array['master']['foretaste']) && $this->bill_array['master']['foretaste'] == 1){
                $arg_array['foretaste'] = 1;
            }
            $goods_array = $quanliGoods->searchGoodsSale($arg_array);
            if (!$goods_array) {
                returnJsonData('商品没有找到或已下架', 404);
            }
            $goods_array = $goods_array[0];


            for($i = 0; $i< $product_code['amount']; $i++){
                $cart_goods_array = $this->createCartGoodsStructure($goods_array);

                if(isset($_REQUEST['bill_type']) && $_REQUEST['bill_type'] == 9){
                    if(isset($cart_goods_array['price']) ){
                       $cart_goods_array['price'] = 0;
                    }
                }


                //切分ID数组
                $cart_goods_array['syncopate'] = explode('|', $cart_goods_array['syncopate']);

                //生日牌ID数组
                $cart_goods_array['birthcard'] = explode('|', $cart_goods_array['birthcard']);

                //=================商品配件===============
                $arg_array = array(
                    'postID' => $cart_goods_array['postID']
                );
                $goods_fittings_relation_array = $goodsFittings->searchGoodsFitsRelation( $arg_array );
                $cart_goods_array['fittings'] = $goods_fittings_relation_array;
                $cart_goods_array['adjustPrice'] = -1;

                $this->bill_array['goods']['general'][$this->bill_array['goods_index']] = $cart_goods_array;

                //试吃订单
                if(isset($this->bill_array['master']['foretaste']) && $this->bill_array['master']['foretaste'] == 1){
                    $cart_goods_array['price'] = '0.00';
                }

                //市场推广类型、补偿订单类型、招待赠送订单类型、试吃订单类型 add by allen.qiang 2015-06-23
                if(isset($_REQUEST['bill_type']) && !empty($_REQUEST['bill_type']) && in_array($_REQUEST['bill_type'],array(3,5,7,9))){
                    $cart_goods_array['price'] = '0.00';
                }

                $return_array['goods'][$this->bill_array['goods_index']] = $cart_goods_array;

                $this->bill_array['goods_index']++;

                //===============现金卡===================

                foreach($this->bill_array['goods']['general'] as &$goods) {

                    $goods['use_card'] = array();


                    $goods['card_deducation'] = 0;


                }

                if(isset($this->bill_array['use_card']['detail']) ){

                    if($this->bill_array['use_card']['balance'] > 0){
                        $use_card = &$this->bill_array['use_card']['detail'];
                        $new_use_card_array = array(
                            'balance' => 0,
                            'detail' => array()
                        );

                        foreach($use_card as &$card) {
                            //验证是否已经使用
                            $result_array = $quanliCard->checkCard( $card['card_pwd'], $this->bill_array['goods']['general'], $new_use_card_array );

                            if($result_array['code'] == 200){
                                $card_pwd = $result_array['data'];
                                $this->bill_array['card_array'][$card_pwd] = $card_pwd;
                            }
                        }
                        $this->bill_array['use_card'] = $new_use_card_array;
                        $return_array['card_array'] = $new_use_card_array['detail'];
                    }else{
                        $return_array['card_array'] = $this->bill_array['use_card']['detail'];
                    }
                }



            }
        };



        $this->setBillArray( $return_array );

    }

    /**
     * @return array
     * 计算配送时间
     */
    private function deliveryTime(){

        quanli_require('module/base/goods/goodsDeliver.class.php');

        //实例化商品配送时间类
        $goodsDeliver = new goodsDeliver();

        //实例化字典类
        $itemHandler = new itemHandler();

        $return_array = array(
            'send_date' => date('Y-m-d'),           //配送日期
            'start_time' => date('Y-m-d'),          //最晚开始配送时间
            'end_time' => '',            //最早结束配送时间
            'cutTime' => ''             //截单时间
        );

        //配送开始时间查询参数
        $arg_array = array(
            'itemCategoryID' => 9,
            'useFlg' => 1
        );

        //配送开始时间
        $start_time_array = $itemHandler->searchItem( $arg_array );

        //配送结束时间查询参数
        $arg_array['itemCategoryID'] = 10;

        //配送结束时间
        $end_time_array = $itemHandler->searchItem( $arg_array );

        $return_array['start_time_array'] = $start_time_array;
        $return_array['end_time_array'] = $end_time_array;

        $bill_goods_array = &$this->bill_array['goods']['general'];

        $max_interval = 0;

        //当天统计
        $current_count = 0;

        foreach($bill_goods_array as $goods){

            $interval = $goods['deliverSpace'];

            if($interval == 0){

                //维护配送时间表
                $arg_array = array(
                    'postID' => $goods['postID'],
                    'nowTime' => date('H:i:s')
                );

                $deliver_relation_array = $goodsDeliver->searchGoodsDeliverRelation( $arg_array );

                if($goods['endDeliver'] < date('H:i:s') || !$deliver_relation_array){
                    $interval = 1;
                }
            }

            //初始化隔天配送的开始配送时间和结束配送时间
            if($max_interval == 0 && $interval > 0){
                $return_array['start_time'] = $goods['startDeliver'];
                if($goods['deliverSpace'] > 0){
                    $return_array['end_time'] = $goods['cutTime'];
                    $return_array['cutTime'] = $goods['cutTime'];
                }else{
                    $return_array['end_time'] = $goods['endDeliver'];
                }
            }

            //最大配送间隔
            if( $interval > 0 && $interval > $max_interval ){
                $max_interval = $interval;
            }

            if($goods['deliverSpace'] > 0){
                if($goods['cutTime'] > $return_array['cutTime']){
                    $return_array['cutTime'] = $goods['cutTime'];
                }
            }

            //隔天配送下最晚的配送开始时间和最早的截单时间
            if($interval > 0 && $max_interval > 0){

                if($goods['startDeliver'] > $return_array['start_time']){
                    $return_array['start_time'] = $goods['startDeliver'];
                }

                if($goods['deliverSpace'] < 1 && $return_array['cutTime'] == ''){
                    if($goods['endDeliver'] > $return_array['end_time']){
                        $return_array['end_time'] = $goods['end_time'];
                    }
                }
            }

            //根据间隔时间得到最晚的配送日期
            if ($interval > 0) {

                $send_date = date('Y-m-d',strtotime("+$interval day"));
                if ($send_date > $return_array['send_date']) {
                    $return_array['send_date'] = $send_date;
                }

            }

            if($max_interval ==0 && $interval < 1){

                $deliver_relation_array = $deliver_relation_array[0];
                if($current_count == 0){

                    $return_array['start_time'] = $deliver_relation_array['startDeliverTime'];
                    $return_array['end_time'] = $deliver_relation_array['endDeliverTime'];
                }else{

                    if($deliver_relation_array['startDeliverTime'] > $return_array['start_time']){
                        $return_array['start_time'] = $deliver_relation_array['startDeliverTime'];
                    }

                    if($deliver_relation_array['endDeliverTime'] > $return_array['end_time']){
                        $return_array['end_time'] = $deliver_relation_array['endDeliverTime'];
                    }
                }
                $current_count++;
            }
        }

        $this->bill_array['master']['send_date'] = $return_array['send_date'];
        $this->bill_array['master']['send_start_time'] = $return_array['start_time'];
        $this->bill_array['master']['send_end_time'] = $return_array['end_time'];

        return $return_array;

    }

    /*
     * 检查配送时间
     */
    private function checkDeliveryTime(){

        if(!isset($_REQUEST['send_date']) || empty($_REQUEST['send_date'])){
            returnJsonData('配送日期必须选择');
        }

        if(!isset($_REQUEST['send_start_time']) || empty($_REQUEST['send_start_time'])){
            returnJsonData('配送开始时间必须选择');
        }

        if (isset($_REQUEST['send_end_time']) && !empty($_REQUEST['send_end_time'])){
            if(strtotime($_REQUEST['send_end_time']) < strtotime($_REQUEST['send_end_time'])){
                returnJsonData('配送结束时间不能小于配送开始时间');
            }
        }

        $arg_array = array(
            'send_date' => $_REQUEST['send_date'],
            'send_start_time' => $_REQUEST['send_start_time'],
            'send_end_time' => $_REQUEST['send_end_time']
        );

        $this->bill_array['master']['send_date'] = $arg_array['send_date'];
        $this->bill_array['master']['send_start_time'] = $arg_array['send_start_time'];
        $this->bill_array['master']['send_end_time'] = $arg_array['send_end_time'];

        $this->setBillArray($arg_array);

    }

    /**
     * 查询二级商品信息
     */
    private function goodsList() {

        //检查商品编号和商品名称
        if (!isset($_REQUEST['product_code'], $_REQUEST['goods_name']) || (empty($_REQUEST['product_code']) && empty($_REQUEST['goods_name']))) {
            returnJsonData('参数错误');
        }

        if(!isset($_REQUEST['cityID']) || empty($_REQUEST['cityID'])){
            returnJsonData('参数错误');
        }

        global $G_SHOP;

        $product_code = $_REQUEST['product_code'];
        $goods_name = $_REQUEST['goods_name'];

        $quanliGoods = new quanliGoods( $G_SHOP->DBCA );

        //查询二级商品信息
        $arg_array = array(
            'product_code' => $product_code,
            'goodsName' => $goods_name,
            'cityID' => $_REQUEST['cityID'],
            'forsale' => 1
        );

        //试吃订单
        if(isset($this->bill_array['master']['foretaste']) && $this->bill_array['master']['foretaste'] == 1){
            $arg_array['foretaste'] = 1;
        }
        $goods_list_array = $quanliGoods->searchList( $arg_array );
        if (!$goods_list_array) {
            returnJsonData('商品没有找到或已下架', 404);
        }

        $goods_array = array();

        foreach($goods_list_array as &$goods_list) {

            //查询三级商品
            $arg_array = array(
                'twoPostID' => $goods_list['postID'],
                'forsale' => 1
            );
            if(isset($this->bill_array['master']['foretaste']) && $this->bill_array['master']['foretaste'] == 1){
                $arg_array['foretaste'] = 1;
            }

            $goods_sale_array = $quanliGoods->searchGoodsSale( $arg_array );
            if (!$goods_sale_array) {
                continue;
            }

            //组装三级商品信息
            $tmp_sale_array = array();
            foreach($goods_sale_array as &$goods_sale) {
                $tmp_sale_array[] = array(
                    'postID' => $goods_sale['postID'],
                    'product_code' => $goods_sale['product_code'],
                    'goodsName' => $goods_sale['goodsName'],
                    'spec' => $goods_sale['spec']
                );
            }

            //组装返回的商品信息
            $goods_array[] = array(
                'postID' => $goods_list['postID'],
                'goodsName' => $goods_list['goodsName'],
                'spec' => $goods_list['spec'],
                'list' => $tmp_sale_array
            );

        }
        if (empty($goods_array)) {
            returnJsonData('商品没有找到或已下架', 404);
        }

        returnJsonData('ok', 200, $goods_array);

    }

    /**
     * 更新购物车普通商品
     */
    private function updateGeneralGoods() {

        //检查网购号
        if (!isset($_REQUEST['postID']) || !is_numeric($_REQUEST['postID'])) {
            $this->toReturn('参数错误');
        }

        //检查更新数量
        if (!isset($_REQUEST['update_amount']) || !in_array($_REQUEST['update_amount'], array(-1, 1))) {
            $this->toReturn('参数错误');
        }

        $postID = $_REQUEST['postID'];
        $update_amount = $_REQUEST['update_amount'];
        $cart_goods_array = &$this->bill_array['goods']['general'];

        if (isset($cart_goods_array[$postID])) {

            $cart_goods_array[$postID]['goods_amount'] += $update_amount;
            $return_goods_array = array(
                'postID' => $postID,
                'goods_amount' => $cart_goods_array[$postID]['goods_amount']
            );

            if ($cart_goods_array[$postID]['goods_amount'] == 0) {
                unset($cart_goods_array[$postID]);
            }

        } else {
            $return_goods_array = array(
                'postID' => $postID,
                'goods_amount' => 0
            );
        }

        $this->setBillArray( $return_goods_array );

    }

    /**
     * 删除购物车普通商品
     */
    private function deleteGeneralGoods() {

        //检查网购号
        if (!isset($_REQUEST['indexKey']) || !is_numeric($_REQUEST['indexKey'])) {
            $this->toReturn('参数错误');
        }

        $indexKey = $_REQUEST['indexKey'];

        //加载类文件
        quanli_include('module/base/promotion/quanliCard.class.php');

        //实例化现金卡类
        $quanliCard = new quanliCard();

        if (isset($this->bill_array['goods']['general'][$indexKey])) {
            unset($this->bill_array['goods']['general'][$indexKey]);
            unset($this->bill_array['card_array']);
        }

        //返回数组
        $return_array = array(
            'send_date' => date("Y-m-d"),
            'card_array' => array(),
        );

        //检查现金卡
        foreach($this->bill_array['goods']['general'] as &$goods) {
            $goods['use_card'] = array();
            $goods['card_deducation'] = 0;
        }

        if(isset($this->bill_array['use_card']['detail'])){
            $use_card = &$this->bill_array['use_card']['detail'];

            if (count($use_card) > 0) {
                $new_use_card_array = array(
                    'balance' => 0,
                    'detail' => array()
                );

                foreach($use_card as &$card) {

                    //验证是否已经使用
                    $result_array = $quanliCard->checkCard( $card['card_pwd'], $this->bill_array['goods']['general'], $new_use_card_array );
                    if($result_array['code'] == 200){
                        $card_pwd = $result_array['data'];
                        $this->bill_array['card_array'][$card_pwd] = $card_pwd;
                    }
                }
                $this->bill_array['use_card'] = $new_use_card_array;
                $return_array['card_array'] = $new_use_card_array['detail'];

            }

        }

        $this->setBillArray( $return_array );
    }


    /**
     * 商品配件修改
     */
    private function fittingsEdit(){

        global $G_SHOP;

        if(!isset($_REQUEST['buyNum'])){
            returnJsonData('参数错误');
        }

        if(!isset($_REQUEST['goods_index']) || !is_numeric($_REQUEST['goods_index'])){
            returnJsonData('参数错误');
        }

        $del_array = array();

        if(isset($_REQUEST['del_array']) && is_array($_REQUEST['del_array'])){
            $del_array = $_REQUEST['del_array'];
        }

        //加载类文件
        quanli_include('module/base/promotion/quanliCard.class.php');

        //实例化现金卡类
        $quanliCard = new quanliCard( $G_SHOP->DBCA );

        $buyNum_array = explode(',',$_REQUEST['buyNum']);

        $goods_index = $_REQUEST['goods_index'];

//        foreach($buyNum_array as $key=>$v){
//            $this->bill_array['goods']['general'][$goods_index]['fittings'][$key]['buyNum'] = $v;
//        }

        $tmp_buyNum_index = 0;
        foreach($this->bill_array['goods']['general'][$goods_index]['fittings'] as $key=>&$fittings){
            $fittings['isdelete'] = 0;
            if(!in_array($key,$del_array))
            {
                $fittings['buyNum'] = $buyNum_array[$tmp_buyNum_index];
            }
            $tmp_buyNum_index ++;
        }

        //add by yim.hu FUN-BK-0009 改 start
        //重新算一下金额
        $price_array = explode(',',$_REQUEST['price']);
        foreach($price_array as $key=>$v){
		//$this->bill_array['goods']['general'][$goods_index]['fittings'][$key]['price'] = $v;
        	$this->bill_array['goods']['general'][$goods_index]['fittings'][$key]['buyMoney'] = $v;
        }	
        //add by yim.hu FUN-BK-0009 修改价格 end
        //删除配件
        foreach($del_array as $del){
            unset($this->bill_array['goods']['general'][$goods_index]['fittings'][$del]);
        }

        //error_log(print_r($this->bill_array['goods']['general'][$goods_index]['fittings'],1));

     
        $return_array = array();

        //现金卡
        foreach($this->bill_array['goods']['general'] as &$goods) {
            $goods['use_card'] = array();
            $goods['card_deducation'] = 0;
        }

        if(isset($this->bill_array['use_card']['detail'])){

            $use_card = &$this->bill_array['use_card']['detail'];
            if (count($use_card) > 0) {

                $new_use_card_array = array(
                    'balance' => 0,
                    'detail' => array()
                );

                foreach($use_card as &$card) {
                    //验证是否已经使用
                    $result_array = $quanliCard->checkCard( $card['card_pwd'], $this->bill_array['goods']['general'], $new_use_card_array );
                    if($result_array['code'] == 200){
                        $card_pwd = $result_array['data'];
                        $this->bill_array['card_array'][$card_pwd] = $card_pwd;
                    }
                }
                $this->bill_array['use_card'] = $new_use_card_array;
                $return_array = $new_use_card_array['detail'];

            }

        }

        $this->setBillArray($return_array);

    }

    /**
     * 手动调价
     */
    private function adjustPrice(){

        global $G_SHOP;

        if(isset($_REQUEST['adjustPrice']) && !empty($_REQUEST['adjustPrice']) && !is_money($_REQUEST['adjustPrice'])){
            returnJsonData('输入的金额不正确，请重新输入');
        }

        if(!isset($_REQUEST['goods_index']) || empty($_REQUEST['goods_index'])){
            returnJsonData('参数错误');
        }

        if(!isset($_REQUEST['code']) || empty($_REQUEST['code'])){
            returnJsonData('缺少参数');
        }

        //加载类文件
        quanli_include('module/base/promotion/quanliCard.class.php');
        quanli_include('module/base/promotion/quanliCoupon.class.php');

        //实例优惠券类
        $quanliCoupon = new quanliCoupon( $G_SHOP->DBCA );

        //实例化现金卡类
        $quanliCard = new quanliCard( $G_SHOP->DBCA );

        $goods_index = $_REQUEST['goods_index'];
        $adjustPrice = $_REQUEST['adjustPrice'];
        $return_array = array(
            'card_array' => array(),
            'coupon_code'=> ''
        );

        //验证
        $result = $G_SHOP->sessionCache_get( 'AUTH_' . $_REQUEST['code'], true );
        if(!$result){
            returnJsonData('权限不够');
        }

        $this->bill_array['goods']['general'][$goods_index]['adjustPrice'] = $adjustPrice;
        $return_array['goods_index'] = $goods_index;
        $return_array['adjustPrice'] = $adjustPrice;

        //检查代金卡
        foreach($this->bill_array['goods']['general'] as &$goods) {
            $goods['use_card'] = array();
            $goods['card_deducation'] = 0;
        }

        if(isset($this->bill_array['use_card']['detail'])){
            $use_card = &$this->bill_array['use_card']['detail'];

            if (count($use_card) > 0) {
                $new_use_card_array = array(
                    'balance' => 0,
                    'detail' => array()
                );

                foreach($use_card as &$card) {
                    //验证是否已经使用
                    $result_array = $quanliCard->checkCard( $card['card_pwd'], $this->bill_array['goods']['general'], $new_use_card_array );
                    if($result_array['code'] == 200){
                        $card_pwd = $result_array['data'];
                        $this->bill_array['card_array'][$card_pwd] = $card_pwd;
                    }
                }
                $this->bill_array['use_card'] = $new_use_card_array;
                $return_array['card_array'] = $new_use_card_array['detail'];
            }
        }

        //检查优惠券
        if(isset($this->bill_array['use_coupon'])){

            $coupon_code = $this->bill_array['use_coupon']['code'];
            foreach ($this->bill_array['goods']['general'] as &$list) {

                $goods_price = $list['price'];
                if(!empty($list['adjustPrice']) && $list['adjustPrice']> -1){
                    $goods_price = $list['adjustPrice'];
                }

                $goods_array[] = array(
                    'postID' => $list['postID'],
                    'price' => $goods_price,
                    'goodsAmount' => 1
                );

            }

            $result_array = $quanliCoupon->checkCoupon($coupon_code, $this->pcustID, $goods_array);
            if ($result_array['code'] == 200) {
                $this->bill_array['use_coupon'] = array(
                    'code' => $result_array['data']['code'],
                    'amount' => $result_array['data']['amount'],
                    'couponID' => $result_array['data']['couponID'],
                    'couponType' => $result_array['data']['couponType']
                );
                $return_array['coupon_code'] = $this->bill_array['use_coupon']['code'];
            }else{
                unset($this->bill_array['use_coupon']);
            }
        }

        $this->setBillArray($return_array);

    }



}